/**
 * cn.ucox.web.framework.handler.UcoxMappingExceptionResolver
 *
 * @author chenw
 * @create 16/3/21.09:38
 * @email javacspring@hotmail.com
 */

package cn.com.oceansoft.framework.resolver;

import cn.com.oceansoft.framework.exception.BizException;
import cn.com.oceansoft.framework.exception.DAOException;
import cn.com.oceansoft.framework.exception.ForbidenAccessException;
import cn.com.oceansoft.framework.kits.RequestKit;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.NoHandlerFoundException;
import org.springframework.web.servlet.handler.SimpleMappingExceptionResolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 全局异常处理解析
 *
 * @author chenw
 * @create 16/3/21 09:38
 * @email javacspring@gmail.com
 */
public class UcoxMappingExceptionResolver extends SimpleMappingExceptionResolver {

//    Logger logger = LoggerFactory.getLogger(getClass());

    @Override
    protected ModelAndView doResolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
        if (RequestKit.isAccessJson(request) || (RequestKit.isAjax(request) && RequestKit.isAccessJson(request))) {
            return getAjaxModelAndView(ex);
        } else {
            String viewName = determineViewName(ex, request);
            if (viewName != null) {
                Integer statusCode = determineStatusCode(request, viewName);
                if (statusCode != null) {
                    applyStatusCodeIfPossible(request, response, statusCode);
                }
                ModelAndView mav = getModelAndView(viewName, ex, request);
                if (!this.logger.isDebugEnabled()) {
                    mav.getModel().remove("exception");
                }
                return mav;
            } else {
                return null;
            }
        }
    }

    private ModelAndView getAjaxModelAndView(Exception exception) {
        ModelAndView mav = new ModelAndView();
        mav.addObject("succ", false);
        if (exception instanceof ForbidenAccessException) {//403
            mav.addObject("code", 403);
            mav.addObject("title", "资源禁止访问");
            mav.addObject("msg", "资源禁止访问");
        } else if (exception instanceof NoHandlerFoundException) {//404
            mav.addObject("code", 404);
            mav.addObject("title", "您访问的资源不存在!");
            mav.addObject("msg", "您访问的资源不存在");
        } else if (exception instanceof HttpRequestMethodNotSupportedException) {//405
            mav.addObject("code", 405);
            mav.addObject("title", "请求方法不支持!");
            mav.addObject("msg", "请求方法不支持");
        } else if (exception instanceof BizException) {//业务异常
            mav.addObject("code", 5001);
            mav.addObject("title", "系统业务操作失败!");
            mav.addObject("msg", "系统业务操作失败");
        } else if (exception instanceof DAOException) {
            mav.addObject("code", 6001);
            mav.addObject("title", "数据访问错误!");
            mav.addObject("msg", "数据访问错误");
        } else {//500 其他异常
            mav.addObject("code", 500);
            mav.addObject("title", "服务器内部错误!");
            mav.addObject("msg", "服务器内部错误!");
        }
        if (this.logger.isDebugEnabled()) {
            mav.addObject("exception", exception.getMessage());
        }
        return mav;
    }

}
